package com.test.webmanage.controller;

import com.test.webmanage.pojo.Emp;
import com.test.webmanage.pojo.Result;
import com.test.webmanage.service.EmpService;
import com.test.webmanage.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
      Emp emps = empService.login(emp);
      if (emps!=null) {
          Map<String, Object> claims = new HashMap<>();
          claims.put("id", emps.getId());
          claims.put("username", emps.getUsername());
          claims.put("name", emps.getName());
          String jwt = JwtUtils.generateJwt(claims);
          return Result.success(jwt);

      }
      return Result.error("用户名或密码错误");
    }
}
